"""
  解析器，解析下载好的网页内容
  create by 01 on 2020-02-29
"""
__author__ = '01'

import re
import urllib.parse

from bs4 import BeautifulSoup

import utils


class HtmlParser(object):

    def parse_root_page(self, page_url, html_cont):
        """
        解析入口页面中的url
        :param page_url:
        :param html_cont:
        :return:
        """
        if page_url is None or html_cont is None:
            return

        soup = BeautifulSoup(html_cont, 'html.parser')
        new_urls = self._get_new_urls(page_url, soup)

        return new_urls

    def parse(self, page_url, html_cont):
        """
        解析下载好的网页内容
        :param page_url: 页面url
        :param html_cont: 网页内容
        :return: 返回新的url列表及解析后的数据
        """
        if page_url is None or html_cont is None:
            return None, None

        soup = BeautifulSoup(html_cont, 'html.parser')
        # 港澳台地区的不纳入统计
        if self.is_domestic(soup) is False:
            return None, None

        new_urls = self._get_new_urls(page_url, soup)
        new_data = self._get_new_data(page_url, soup)

        return new_urls, new_data

    @staticmethod
    def _get_new_urls(page_url, soup):
        """
        得到新的url列表
        :param page_url:
        :param soup:
        :return:
        """
        new_urls = set()
        # 视频页面URL：//www.bilibili.com/bangumi/play/ss28320
        links = soup.find_all('a', href=re.compile(r"/bangumi/play/ss(.*)"))
        for link in links:
            new_url = link['href']
            # 拼接成完整的url
            new_full_url = urllib.parse.urljoin(page_url, new_url)
            new_urls.add(new_full_url)

        return new_urls

    @staticmethod
    def _get_new_data(page_url, soup):
        """
        解析数据，并返回解析后的数据
        :param page_url:
        :param soup:
        :return:
        """
        # 使用字典来存放解析后的数据
        res_data = dict()
        res_data['url'] = page_url
        res_data['title'] = HtmlParser.get_title(soup)
        res_data['score'] = HtmlParser.get_score(soup)
        res_data['score_number'] = HtmlParser.get_score_number(soup)

        return res_data

    @staticmethod
    def get_title(soup):
        # 标题标签格式：<a href=** title=** target** class="media-title">{title}</a>
        title_node = soup.find('a', class_='media-title')
        return title_node.get_text() if title_node else '暂无标题'

    @staticmethod
    def get_score(soup):
        # 评分标签格式：<h4 class ="score">{score}</h4>
        score = soup.find('h4', class_='score')
        score_text = score.get_text() if score else '暂无评分'
        # 转换成浮点类型，如果是暂无评分则做0处理
        return 0.0 if utils.is_chinese(score_text) else float(score_text)

    @staticmethod
    def get_score_number(soup):
        # 评分人数标签格式：<div class="media-rating">**<p>2.8万人评分</p></div>
        score_number = soup.find('div', class_='media-rating')
        return score_number.find('p').get_text() if score_number else '暂无评分人数'

    @staticmethod
    def is_domestic(soup):
        """
        判断该番剧是否是港澳台地区的
        :param soup:
        :return: True or False
        """
        title = HtmlParser.get_title(soup)
        return title.find('僅限') == -1
